import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import _CircularProgress from "@mui/material/CircularProgress";
import _useMediaQuery from "@mui/material/useMediaQuery";
import React from "react";
import instance from "../../instance";
import { useExternal, useMount } from "ahooks";
import classes from "./paypalButton.module.less";
import { useSelector } from "react-redux";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
// type PayConfigKey = keyof typeof instance.payConfig;
// const payConfig = new Proxy(instance.payConfig, {
//   get: (_, attr: PayConfigKey) => {
//     return instance.payConfig[attr];
//   },
// });
// const sdk = `https://www.paypal.com/sdk/js?client-id=${payConfig.CLIENT_ID}&currency=EUR,USD,CAD&component=buttons,applepay&disable-funding=paylater,mybank,giropay,ideal,eps,blik,mercadopago`;
var delay = function(wait) {
    return new Promise(function(resolve) {
        return setTimeout(resolve, wait);
    });
};
function PaypalButton(props) {
    var status = useExternal(instance.payConfig.paypalUri, {
        type: "js",
        keepWhenUnused: true
    });
    var isPc = _useMediaQuery("(min-width:1280px)");
    var theme = useSelector(function(rootState) {
        return rootState.app.theme;
    });
    var onApprove = function() {
        var _ref = _async_to_generator(function() {
            var _props_onError, _props_onApprove;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        return [
                            4,
                            delay(1000)
                        ];
                    case 1:
                        _state.sent();
                        if (Math.random() > 0.5) {
                            ;
                            (_props_onError = props.onError) === null || _props_onError === void 0 ? void 0 : _props_onError.call(props);
                        } else {
                            ;
                            (_props_onApprove = props.onApprove) === null || _props_onApprove === void 0 ? void 0 : _props_onApprove.call(props);
                        }
                        return [
                            2
                        ];
                }
            });
        });
        return function onApprove() {
            return _ref.apply(this, arguments);
        };
    }();
    var onCancel = function() {
        var _props_onCancel;
        (_props_onCancel = props.onCancel) === null || _props_onCancel === void 0 ? void 0 : _props_onCancel.call(props);
    };
    useMount(function() {
        if (status === "ready" && window.paypal) {
            window.paypal.Buttons({
                style: {
                    tagline: false,
                    color: theme == "dark" ? "black" : "white"
                },
                createOrder: function() {},
                onApprove: onApprove,
                oncancel: onCancel
            }).render("#paypal-button-container");
        }
    });
    return /*#__PURE__*/ _jsxs("div", {
        className: classes.wrap,
        children: [
            status === "loading" && /*#__PURE__*/ _jsx(_CircularProgress, {
                size: !isPc ? "8vw" : "38px",
                style: {
                    color: theme == "dark" ? "#fff" : "var(--primary)"
                },
                color: "inherit"
            }),
            status === "ready" && /*#__PURE__*/ _jsx("div", {
                id: "paypal-button-container",
                className: classes.btn_container
            }),
            status === "error" && "error",
            status === "unset" && "unset"
        ]
    });
}
export default PaypalButton;

 //# sourceMappingURL=index.js.map